﻿using System;
using System.Collections.Generic;
using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.GPS;

namespace LinuxGarminImport
{
    public class ActivityContainer
    {
        #region TrackData

        public GPSRoute GpsRoute
        {
            get { return gpsRoute; }
        }

        public INumericTimeDataSeries HeartRateData
        {
            get { return hrData; }
        }

        public IDistanceDataTrack DistanceData
        {
            get { return distanceData; }
        }

        public INumericTimeDataSeries CadenceData
        {
            get { return cadenceData; }
        }

        public INumericTimeDataSeries AltitudeData
        {
            get { return altitudeData; }
        }

        public INumericTimeDataSeries PowerData
        {
            get { return powerData; }
        }

        public void AddPause(DateTime lower, DateTime upper)
        {
            timerPauses.Add(new ValueRange<DateTime>(lower, upper));
        }

        public ValueRangeSeries<DateTime> TimerPauses
        {
            get
            {
                return timerPauses;
            }
        }

        public DateTime StartTime
        {
            get
            {
                DateTime startTime = gpsRoute.StartTime;

                if (startTime.CompareTo(hrData.StartTime) < 0 && hrData.StartTime != DateTime.MinValue)
                {
                    startTime = hrData.StartTime;
                }

                if (startTime.CompareTo(distanceData.StartTime) < 0 && distanceData.StartTime != DateTime.MinValue)
                {
                    startTime = distanceData.StartTime;
                }
                if (startTime.CompareTo(cadenceData.StartTime) < 0 && cadenceData.StartTime != DateTime.MinValue)
                {
                    startTime = cadenceData.StartTime;
                }

                if (startTime.CompareTo(altitudeData.StartTime) < 0 && altitudeData.StartTime != DateTime.MinValue)
                {
                    startTime = altitudeData.StartTime;
                }

                if (startTime.CompareTo(powerData.StartTime) < 0 && powerData.StartTime != DateTime.MinValue)
                {
                    startTime = powerData.StartTime;
                }

                return startTime;
            }
        }

        public bool ValidImport
        {
            get
            {
                // If any data tracks are populated, then it's ok for import
                if (gpsRoute.Count > 0 || hrData.Count > 0 || distanceData.Count > 0 || cadenceData.Count > 0 || powerData.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        //private DateTime hrTime = DateTime.MinValue;

        private INumericTimeDataSeries hrData = new NumericTimeDataSeries();
        private IDistanceDataTrack distanceData = new DistanceDataTrack();
        private INumericTimeDataSeries cadenceData = new NumericTimeDataSeries();
        private INumericTimeDataSeries altitudeData = new NumericTimeDataSeries();
        private INumericTimeDataSeries powerData = new NumericTimeDataSeries();
        private GPSRoute gpsRoute = new GPSRoute();
        private ValueRangeSeries<DateTime> timerPauses = new ValueRangeSeries<DateTime>();
        #endregion

        public void AddLap(DateTime time, TimeSpan duration, float distance, float calories, string intensity)
        {
            Lap lap = new Lap(time, duration, distance, calories, intensity);
            laps.Add(lap);
        }

        public IList<Lap> Laps
        {
            get { return laps; }
        }
        private IList<Lap> laps = new List<Lap>();

        #region Lap

        public class Lap
        {
            // TODO: Get rid of most of this since it's not used.  Only StartTime and Duration are used.
            public Lap(DateTime time, TimeSpan duration, float distance, float calories, string intensity)
            {
                startTime = time;
                Duration = duration;
                Distance = distance;
                Calories = calories;
                Intensity = intensity;
            }
            public GPSLocation BeginPos
            {
                get { return beginPos; }
                set { beginPos = value; }
            }

            public GPSLocation EndPos
            {
                get { return endPos; }
                set { endPos = value; }
            }

            public float MaxSpeed
            {
                get { return maxSpeed; }
                set { maxSpeed = value; }
            }

            public float Calories
            {
                get { return calories; }
                set { calories = value; }
            }

            public float AvgHR
            {
                get { return avgHR; }
                set { avgHR = value; }
            }

            public float MaxHR
            {
                get { return maxHR; }
                set { maxHR = value; }
            }

            public float AvgCadence
            {
                get { return avgCadence; }
                set { avgCadence = value; }
            }

            public string Intensity
            {
                get { return intensity; }
                set { intensity = value; }
            }

            public float Distance
            {
                get { return distance; }
                set { distance = value; }
            }

            public DateTime StartTime
            {
                get { return startTime; }
                set { startTime = value; }
            }

            public TimeSpan Duration
            {
                get { return duration; }
                set { duration = value; }
            }

            private GPSLocation beginPos;
            private GPSLocation endPos;
            private float maxSpeed;
            private float calories;
            private float avgHR;
            private float maxHR;
            private float avgCadence;
            private string intensity;
            private float distance;

            private DateTime startTime = DateTime.MinValue;
            private TimeSpan duration = TimeSpan.Zero;
        }
        #endregion

    }
}